'use server'

import { redirect } from 'next/navigation'
import { addNote, updateNote, delNote } from '@/lib/prisma';
import { revalidatePath } from 'next/cache';
import { sleep } from '@/lib/utils';
import { z } from 'zod'

const schema = z.object({
  title: z.string().min(1, '请填写内容'),
  content: z.string().min(1, '请填写内容').max(100, '字数最多 100')
});

export async function saveNote(prevState: any, formData: any) {

  const noteId = formData.get('noteId')

  const data = {
    title: formData.get('title'),
    content: formData.get('body'),
    updateTime: new Date()
  }

  // zod校验数据
  const validated = schema.safeParse(data)
  if (!validated.success) {
    return {
      errors: validated.error.issues,
    }
  }

  // 为了让效果更明显
  await sleep(2000)

  if (noteId) {
    updateNote(noteId, JSON.stringify(data))
    // revalidatePath( path : string , type? : "page" | "layout" )
    revalidatePath('/', 'layout')
  } else {
    const res = await addNote(JSON.stringify(data))
    revalidatePath('/', 'layout')
  }
  return { message: `Add Success!` }
}

export async function deleteNote(prevState: any, formData: any) {
  const noteId = formData.get('noteId')
  try {

    delNote(noteId)
  } catch (e) {
    return {
      message: 'delete failed ',
      e,
    }
  }
  revalidatePath('/', 'layout')
  redirect('/')
}

